﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotonJXC.Entity;
using NHibernate.Criterion;
using BotonJXC.Util;

namespace BotonJXC.DAO
{
    public class OutStocksDAO:NHibernateDao<OutStocks,int>
    {
        public void saveOutStocks(OutStocks outstocks)
        {
            base.Save(outstocks);
        }

        /// <summary>
        /// 根据条件查询出库单
        /// </summary>
        /// <param name="begintime"></param>
        /// <param name="endtime"></param>
        /// <param name="outstocks"></param>
        /// <param name="start"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public PageModel<OutStocks> findOutStocksByCondition(DateTime begintime, DateTime endtime, OutStocks outstocks, int start, int count)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (begintime != null)
            {
                criterions.Add(Restrictions.Ge("CreateTime", begintime));
            }
            if (endtime != null)
            {
                criterions.Add(Restrictions.Le("CreateTime", endtime));
            }
            if (outstocks != null)
            {
                if (Utils.IsNotBlank(outstocks.OutSn))
                {
                    criterions.Add(Restrictions.Like("OutSn", outstocks.OutSn, MatchMode.Anywhere));
                }

                if (Utils.IsNotBlank(outstocks.AuditStatus))
                {
                    criterions.Add(Restrictions.Eq("AuditStatus", outstocks.AuditStatus));
                }
                if (outstocks.OrgId > 0)
                {
                    criterions.Add(Restrictions.Eq("OrgId", outstocks.OrgId));
                }
                if (outstocks.OrgDepId > 0)
                {
                    criterions.Add(Restrictions.Eq("OrgDepId", outstocks.OrgDepId));
                }
                if (outstocks.OutWarehouse != null )
                {
                    criterions.Add(Restrictions.Eq("outstocks.OutWarehouse", outstocks.OutWarehouse.WarehouseId));
                }
                if (outstocks.InWarehouse!=null)
                {
                    criterions.Add(Restrictions.Eq("outstocks.InWarehouse", outstocks.InWarehouse.WarehouseId));
                }
                if (Utils.IsNotBlank(outstocks.OutType))
                {
                    criterions.Add(Restrictions.Eq("OutType", outstocks.OutType));
                }

                if (outstocks.Orders != null && Utils.IsNotBlank(outstocks.Orders.OrderSn))
                {
                    criterions.Add(Restrictions.Eq("Orders.OrderSn", outstocks.Orders.OrderSn));
                }
                if (  Utils.IsNotBlank( outstocks.CreatePlace ))
                {
                    criterions.Add(Restrictions.Eq("CreatePlace", outstocks.CreatePlace));
                }
            }

            Order order = new Order("CreateTime", false);

            return base.FindByPage(criterions.ToArray(), order, start, count);
        }
        public PageModel<OutStocks> findOutStocksByCondition(  OutStocks outstocks, int start, int count)
        {
            List<ICriterion> criterions = new List<ICriterion>();
             
            if (outstocks != null)
            {
                if (Utils.IsNotBlank(outstocks.OutSn))
                {
                    criterions.Add(Restrictions.Like("OutSn", outstocks.OutSn, MatchMode.Anywhere));
                }

                if (Utils.IsNotBlank(outstocks.AuditStatus))
                {
                    criterions.Add(Restrictions.Eq("AuditStatus", outstocks.AuditStatus));
                }
                if (outstocks.OrgId > 0)
                {
                    criterions.Add(Restrictions.Eq("OrgId", outstocks.OrgId));
                }
                if (outstocks.OrgDepId > 0)
                {
                    criterions.Add(Restrictions.Eq("OrgDepId", outstocks.OrgDepId));
                }
                if (outstocks.OutWarehouse != null)
                {
                    criterions.Add(Restrictions.Eq("outstocks.OutWarehouse", outstocks.OutWarehouse.WarehouseId));
                }
                if (outstocks.InWarehouse != null)
                {
                    criterions.Add(Restrictions.Eq("outstocks.InWarehouse", outstocks.InWarehouse.WarehouseId));
                }
                if (Utils.IsNotBlank(outstocks.OutType))
                {
                    criterions.Add(Restrictions.Eq("OutType", outstocks.OutType));
                }

                if (outstocks.Orders != null && Utils.IsNotBlank(outstocks.Orders.OrderSn))
                {
                    criterions.Add(Restrictions.Eq("Orders.OrderSn", outstocks.Orders.OrderSn));
                }
                if (Utils.IsNotBlank(outstocks.CreatePlace))
                {
                    criterions.Add(Restrictions.Eq("CreatePlace", outstocks.CreatePlace));
                }
            }

            Order order = new Order("CreateTime", false);

            return base.FindByPage(criterions.ToArray(), order, start, count);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderSn"></param>
        /// <returns></returns>
        public IList<OutStocks> findOutStocksBySn(string OutSn, string OutType, string CreatePlace)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (Utils.IsNotBlank(OutSn))
            {
                criterions.Add(Restrictions.Eq("OutSn", OutSn));
            }
            if (Utils.IsNotBlank(OutType))
            {
                criterions.Add(Restrictions.Eq("OutType", OutType));
            }
            if (Utils.IsNotBlank(CreatePlace))
            {
                criterions.Add(Restrictions.Eq("CreatePlace", CreatePlace));
            }
            return base.Find(criterions.ToArray());
        }
    }
}
